route.js 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. // app/api/branches/[branch]/years/route.js
  2. import { NextResponse } from "next/server";
  3. import { listYears } from "@/lib/storage";
  4. import { getSession } from "@/lib/auth/session";
  5. import { canAccessBranch } from "@/lib/auth/permissions";
  6. /**
  7. * GET /api/branches/[branch]/years
  8. */
  9. export async function GET(request, ctx) {
  10. const session = await getSession();
  11. if (!session) {
  12. return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
  13. }
  14. // Next.js 16: params are resolved asynchronously via ctx.params
  15. const { branch } = await ctx.params;
  16. console.log("[/api/branches/[branch]/years] params:", { branch });
  17. if (!branch) {
  18. return NextResponse.json(
  19. { error: "branch Parameter fehlt" },
  20. { status: 400 }
  21. );
  22. }
  23. if (!canAccessBranch(session, branch)) {
  24. return NextResponse.json({ error: "Forbidden" }, { status: 403 });
  25. }
  26. try {
  27. const years = await listYears(branch);
  28. return NextResponse.json({ branch, years });
  29. } catch (error) {
  30. console.error("[/api/branches/[branch]/years] Error:", error);
  31. return NextResponse.json(
  32. { error: "Fehler beim Lesen der Jahre: " + error.message },
  33. { status: 500 }
  34. );
  35. }
  36. }